using UnityEngine;
using QFramework;

namespace ProjectSurvivor
{
    public partial class SimpleAxe : ViewController
    {
        void Start()
        {
            // Code Here
        }

        float mCurrentSeconds = 0;
        void Update()
        {
            mCurrentSeconds += Time.deltaTime;
            if (mCurrentSeconds >= 1.0)
            {
                mCurrentSeconds = 0;
                Axe.Instantiate().Show().Position(this.Position()).Self((self) =>
                {
                    var rigidbody2D = self.GetComponent<Rigidbody2D>();
                    var randomX = RandomUtility.Choose(-8, -5, -3, 3, 5, 8);
                    var randomY = RandomUtility.Choose(3, 5, 8);
                    rigidbody2D.velocity = new Vector2(randomX, randomY);
                    self.OnTriggerEnter2DEvent((collider)=>
                    {
                        var hurtBox = collider.GetComponent<HurtAndHitBox>();
                        if (hurtBox)
                        {
                            
                            if (hurtBox.Owner.CompareTag("Enemy") || hurtBox.Owner.CompareTag("EnemyFly") || hurtBox.Owner.CompareTag("EnemyMiniBoss"))
                            {
                                hurtBox.Owner.GetComponent<Enemy>().Hurt(2);
                                //DemageSystem.CalculateDemage(Global..Value, hurtBox.Owner.GetComponent<IEnemy>());
                            }
                        }
                    }).UnRegisterWhenGameObjectDestroyed(self);

                    ActionKit.OnUpdate.Register(()=>
                    {
                        if(Player.Default)
                        {
                            if(Player.Default.Position().y -  self.Position().y >=15)
                            {
                                self.DestroyGameObjGracefully();
                            }
                        }
                    }).UnRegisterWhenGameObjectDestroyed(self);

                });
            }
           

        }
    }
}
